package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdy.common.utils.DateUtil;
import com.sdy.healthcode.biz.constants.ParamConstants;
import com.sdy.healthcode.biz.dto.holiday.HolidayYearDTO;
import com.sdy.healthcode.biz.model.Holiday;
import com.sdy.healthcode.biz.mapper.HolidayMapper;
import com.sdy.healthcode.biz.service.HolidayService;
import com.sdy.healthcode.biz.service.SysParamService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyh
 * @since 2020-04-13
 */
@Slf4j
@Service
public class HolidayServiceImpl extends BaseServiceImpl<Holiday> implements HolidayService {
    @Autowired
    private HolidayMapper holidayMapper;
    @Autowired
    private SysParamService sysParamService;

    @Override
    @SuppressWarnings("unchecked")
    public HolidayYearDTO getHolidaysForYear(Integer year) {
        if (year == null) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        }
        List<Holiday> l = lambdaQuery()
                .between(Holiday::getDay, String.format("%04d-01-01", year), String.format("%04d-12-31", year))
                .orderByAsc(Holiday::getId)
                .list();
        return HolidayYearDTO.fromHolidayList(year, l);
    }

    @Override
    public void setHolidays(HolidayYearDTO holidays) {
        Integer year = holidays.getYear();
        if (year == null) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        }
        remove(new LambdaQueryWrapper<Holiday>()
                .between(Holiday::getDay, String.format("%04d-01-01", year), String.format("%04d-12-31", year)));
        List<Holiday> toInsertObject = new ArrayList<>();
        for (HolidayYearDTO.HolidayMonthDTO holidayMonthDTO : holidays.getMonths()) {
            if (holidayMonthDTO.getDays() != null) {
                for (Integer day : holidayMonthDTO.getDays()) {
                    Calendar c = Calendar.getInstance();
                    c.set(Calendar.YEAR, year);
                    c.set(Calendar.MONTH, holidayMonthDTO.getMonth() - 1);
                    c.set(Calendar.DATE, day);
                    toInsertObject.add(new Holiday().setDay(c.getTime()));
                }
            }
        }
        if (!toInsertObject.isEmpty()) {
            holidayMapper.insertBatch(toInsertObject);
        }
    }

    @Override
    public Boolean checkScheduleStateForDay(Date date) {
        if (Boolean.parseBoolean(sysParamService.getOrDefault(ParamConstants.SCHEDULE_FOR_HOLIDAY, "false"))) {
            // 节假日排班
            return true;
        }
        return lambdaQuery().eq(Holiday::getDay, DateUtil.getDateBegin(date)).count() <= 0;
    }

    @Override
    public Boolean checkScheduleStateForDay(Date date, Integer departmentId) {
        boolean scheduleForce = Arrays.stream(sysParamService.getOrDefault(ParamConstants.SCHEDULE_FOR_HOLIDAY_FORCE_DEPARTMENT, "")
                .split(","))
                .anyMatch(item -> item.equals(departmentId + ""));
        return scheduleForce ? true : checkScheduleStateForDay(date);
    }
}
